package com.dev;
import android.content.pm.ApplicationInfo;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.fb.FileBrowserPage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class AppListPage extends LinearLayout {
	MainActivity ma;
	ListView listView;
	List<ApplicationInfo> allApps;
	ListView.LayoutParams itemLayoutParams;
	String[] allApps_name;
	AppAdapter aa;
	ArrayList<Integer> userApps=new ArrayList<Integer>();
	ArrayList<Integer> systemApps=new ArrayList<Integer>();
	File[] allApps_archive;
	Drawable[] allApps_icon;
	int whichWillShow=0;
    AppListPage(MainActivity ma) {
		super(ma);
		this.ma = ma;
		setOrientation(VERTICAL);
		aa=new AppAdapter();
		allApps=ma.pm.getInstalledApplications(0);
		allApps_name=new String[allApps.size()];
		allApps_archive=new File[allApps.size()];
		allApps_icon=new Drawable[allApps.size()];
		for(int i=0,size=allApps.size();i<size;i++){
			ApplicationInfo ai=allApps.get(i);
			if((ai.flags&ai.FLAG_SYSTEM)==0){
				userApps.add(i);
			}else{
				systemApps.add(i);
			}
		}
		itemLayoutParams=new ListView.LayoutParams(-1,(int)(27*ma.dp));
		listView=new ListView(ma);
		listView.setAdapter(aa);
		listView.setVerticalScrollBarEnabled(false);
		addView(listView,-1,-1);
	}
    class AppAdapter extends BaseAdapter {
		@Override
		public View getView(int p, View view, ViewGroup viewGroup) {
			if(view==null){
				AppViewItem avi=new AppViewItem();
				avi.setLayoutParams(itemLayoutParams);
				avi.setPackage(p);
				return avi;
			}else if(view instanceof AppViewItem){
				AppViewItem avi=(AppViewItem)view;
				avi.setPackage(p);
				return avi;
			}
			return null;
		}
		@Override
		public int getCount() {
			if(whichWillShow==0){
				return userApps.size();
			}else if(whichWillShow==1){
				return allApps.size();
			}
			return 0;
		}
		@Override
		public Object getItem(int p) {
			return null;
		}
		@Override
		public long getItemId(int p) {
			return 0;
		}
	}
	class AppViewItem extends LinearLayout implements OnClickListener,OnLongClickListener{
		IconView icon;
		LinearLayout linear;
		TextView name;
		TextView info;
		FileBrowserPage.Pressed pressed=new FileBrowserPage.Pressed();
		final static String hint1="安装包大小：";
		volatile ReadPackageAndShow contentLoader;
		volatile ApplicationInfo app;
		volatile int index;
		AppViewItem(){
			super(ma);
			setOrientation(HORIZONTAL);
			setOnClickListener(this);
			setOnLongClickListener(this);
			setBackground(pressed);
			setFocusable(false);
			icon=new IconView(ma);
			icon.setBackgroundColor(0xffaa7fde);
			icon.setOnClickListener(this);
			icon.scaleWidth=1;
			name=new TextView(ma);
			name.setTypeface(Typeface.MONOSPACE);
			name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
			name.setTextColor(0xff000000);
			name.setPadding(12,0,0,0);
			info=new TextView(ma);
			info.setTypeface(Typeface.MONOSPACE);
			info.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10);
			info.setTextColor(0x77888888);
			info.setPadding(12,0,0,0);
			linear=new LinearLayout(ma);
			linear.setOrientation(VERTICAL);
			linear.addView(name,new LayoutParams(-1,0,5));
			linear.addView(info,new LayoutParams(-1,0,4));
			addView(icon,-2,-1);
			addView(linear,new LayoutParams(0,-1,1));
		}
		synchronized void setPackage(int index){
			this.index=index;
			if(whichWillShow==0){
				int index_maped=userApps.get(index);
				app=allApps.get(index_maped);
				if(allApps_name[index_maped]==null){
					contentLoader=new ReadPackageAndShow(this);
					contentLoader.start();
					icon.setImageDrawable(null);
				}else{
					name.setText(allApps_name[index_maped]);
					icon.setImageDrawable(allApps_icon[index_maped]);
					long len=allApps_archive[index_maped].length();
					if(len<1024){
						info.setText(len+"B");
					}else if(len<1024*1024){
						info.setText(hint1+len/1024+"KB");
					}else{
						info.setText(hint1+len/1024/1024+"MB");
					}
				}
			}else if(whichWillShow==1){
				app=allApps.get(index);
				if(allApps_name==null)allApps_name=new String[allApps.size()];
				if(allApps_name[index]==null){
					contentLoader=new ReadPackageAndShow(this);
					contentLoader.start();
					icon.setImageDrawable(null);
				}else{
					name.setText(allApps_name[index]);
					icon.setImageDrawable(allApps_icon[index]);
					long len=allApps_archive[index].length();
					if(len<1024){
						info.setText(hint1+len+"B");
					}else if(len<1024*1024){
						info.setText(hint1+len/1024+"KB");
					}else{
						info.setText(hint1+len/1024/1024+"MB");
					}
				}
			}
		}
		@Override
		public void onClick(View view) {
			ArchiveListTab alt=new ArchiveListTab(ma);
			TabData.ArchiveViewer av=new TabData.ArchiveViewer();
			av.src=new File(app.sourceDir);
			alt.av=av;
			alt.setOnClickListener(ma.gui.TabOnClick);
			alt.setText(app.loadLabel(ma.pm).toString());
			alt.needRemember=false;
			ma.gui.tabs.addView(alt,TabBar.tablp);
			ma.gui.TabOnClick.onClick(alt);
			//ma.tabList.addTabInfo(av);
			//ma.tdm.updateSavedTabList(ma.tabList);
		}
		@Override
		public boolean onLongClick(View view) {
			return true;
		}
	}
	public final class ReadPackageAndShow extends Thread {//there is only one thread on running simultaneously
		final AppViewItem destination;
		final ApplicationInfo app;
		final int index;
		final int whichWillShow;
		ReadPackageAndShow(AppViewItem destination){
			this.destination=destination;
			app=destination.app;
			index=destination.index;
			whichWillShow=AppListPage.this.whichWillShow;
		}
		@Override
		public void run() {
			super.run();
			if(whichWillShow==0){
				int index_maped=userApps.get(index);
				final String label=app.loadLabel(ma.pm).toString();
				final File src=new File(app.sourceDir);
				final Drawable d=app.loadIcon(ma.pm);
				allApps_archive[index_maped]=src;
				allApps_icon[index_maped]=d;
				allApps_name[index_maped]=label;
				if(destination.contentLoader==this){
					post(new Runnable(){
							@Override 
							public void run(){
								destination.setPackage(index);
							}
						});
				}
			}else if(whichWillShow==1){
				if(allApps_icon==null)allApps_icon=new Drawable[allApps.size()];
				if(allApps_archive==null)allApps_archive=new File[allApps.size()];
				final String label=app.loadLabel(ma.pm).toString();
				final File src=new File(app.sourceDir);
				final Drawable d=app.loadIcon(ma.pm);
				allApps_name[index]=label;
				allApps_archive[index]=src;
				allApps_icon[index]=d;
				if(destination.contentLoader==this){
					post(new Runnable(){
						@Override 
						public void run(){
							destination.setPackage(index);
						}
					});
				}
			}
		}
	}
	class QueryActionForApp extends LinearLayout{
		QueryActionForApp(MainActivity ma){
			super(ma);
		}
	}
}
